phpDocumentor Web Commons
[ class tree: Web Commons ] [ index: Web Commons ] [ all elements ]

Source for file controller.php

Documentation is available at controller.php

  1. <?php
  2. /**
  3.  * This file groups classes pertaining to the "controller" of MVC.
  4.  * 
  5.  * @author Antoine d'Otreppe de Bouvette <a.dotreppe@aspyct.org>
  6.  * @license http://www.opensource.org/licenses/mit-license.php
  7.  * @version 0.1dev
  8.  */
  9.  
  10. /**
  11.  * Defines a common base for web controllers.
  12.  * 
  13.  * @since 0.1
  14.  */
  15. abstract class AbstractController {
  16.     /**
  17.      * @var Request 
  18.      */
  19.     private $request;
  20.     
  21.     /**
  22.      * @return Request 
  23.      * @since 0.1
  24.      */
  25.     public function getRequest({
  26.         return $this->request;
  27.     }
  28.     
  29.     /**
  30.      * @param Request $request 
  31.      * @since 0.1
  32.      */
  33.     public function setRequest(Request $request{
  34.         $this->request $request;
  35.     }
  36.     
  37.     /**
  38.      * Finds the action named "do_$action" and runs it with the $request object.
  39.      * Returns the return value of the action. Throws an ActionNotFoundException
  40.      * if the action could not be found.
  41.      * 
  42.      * @param string $action 
  43.      * @param Request $request 
  44.      * @throws ActionNotFoundException
  45.      * @return mixed 
  46.      * @since 0.1
  47.      */
  48.     public function runAction($action{
  49.         $method array($this'do_' $action);
  50.         
  51.         if (is_callable($method)) {
  52.             try {
  53.                 return call_user_func($method);
  54.             }
  55.             catch (Exception $e{
  56.                 throw new ActionException(
  57.                     get_class($this).'::do_'.$action.' thrown an exception',
  58.                     $e
  59.                 );
  60.             }
  61.         }
  62.         else {
  63.             throw new ActionNotFoundException(
  64.                 'Could not find action "' $action '".');
  65.         }
  66.     }
  67. }
  68.  
  69. /**
  70.  * Exception thrown when the requested action could not be found.
  71.  * 
  72.  * @since 0.1
  73.  */
  74. class ActionNotFoundException extends Exception {}
  75.  
  76. /**
  77.  * Exception thrown when an action ran by runAction() thrown an exception
  78.  */
  79. class ActionException extends Exception {
  80.     /**
  81.      * @param string $message 
  82.      * @param Exception $previous 
  83.      */
  84.     public function __construct($messageException $previous{
  85.         parent::__construct($message0$previous);
  86.     }
  87. }

Documentation generated on Fri, 16 Jul 2010 00:48:39 +0200 by phpDocumentor 1.4.3